package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.interfaces.EnsureCallBack;
import com.yy.jhl.ui.main.activity.PhotosShowActivity;
import com.yy.jhl.ui.manager.contract.OutboundCheckContract;
import com.yy.jhl.ui.manager.presenter.OutboundCheckPresenter;
import com.yy.jhl.util.DialogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 出库审核
 * Created by libingn on 2017/12/20.
 */

public class OutboundAfterCheckActivity extends BaseActivity<OutboundCheckPresenter> implements OutboundCheckContract.View {
    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.order_code_text)
    TextView orderCodeText;
    @BindView(R.id.batch_text)
    TextView batchText;
    @BindView(R.id.product_text)
    TextView productText;
    @BindView(R.id.order_product_more_text)
    TextView productMoreText;
    @BindView(R.id.all_num_text)
    TextView allNumText;
    @BindView(R.id.auth_letter_img)
    ImageView authLetterImg;
    @BindView(R.id.company_name_text)
    TextView companyNameText;
    @BindView(R.id.contact_name_text)
    TextView contactNameText;
    @BindView(R.id.contact_tel_text)
    TextView contactTelText;
    @BindView(R.id.contact_note_text)
    TextView contactNoteText;
    @BindView(R.id.time_text)
    TextView timeText;
    @BindView(R.id.check_hint_text)
    TextView checkHintText;
    @BindView(R.id.note_edit)
    EditText noteEdit;
//    @BindView(R.id.again_btn)
//    Button againBtn;
//    @BindView(R.id.no_again_btn)
//    Button noAgainBtn;
//    @BindView(R.id.message_btn)
//    Button messageBtn;
    @BindView(R.id.check_parent)
    LinearLayout checkParent;
    @BindView(R.id.letter_parent)
    LinearLayout letterParent;
    @BindView(R.id.check_result_parent)
    LinearLayout checkResultParent;
    @BindView(R.id.check_result_text)
    TextView checkResultText;
    @BindView(R.id.gnhd_parent)
    LinearLayout gnhdParent;
    @BindView(R.id.gnhd_result_text)
    TextView gnhdResultText;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    @BindView(R.id.check_btn)
    Button checkBtn;
    @BindView(R.id.submit_check_btn)
    Button submitCheckBtn;
    @BindView(R.id.inner_forwarder_parent)
    LinearLayout innerForwarderParent;
    @BindView(R.id.forwarder_name_text)
    TextView forwarderNameText;
    @BindView(R.id.forwarder_contact_text)
    TextView forwarderContactText;
    @BindView(R.id.forwarder_tel_text)
    TextView forwarderTelText;
    LogisticsInfo logisticsInfo;
    //1 :供应商 2：采购商
    private int currentLoginType = 1;
    String status = "";//审核状态1、默认2、通过3、拒绝4、留言
    int checkResultStatus = 0;
    public static void startAction(Context mContext, LogisticsInfo logisticsInfo, String status) {
        Intent intent = new Intent(mContext, OutboundAfterCheckActivity.class);
        intent.putExtra("logisticsInfo", logisticsInfo);
        intent.putExtra("status", status);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_outbound_after_check;
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        logisticsInfo = (LogisticsInfo) getIntent().getSerializableExtra("logisticsInfo");
        status = getIntent().getStringExtra("status");
        showTitle("出库", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        //审核状态1、默认2、通过3、拒绝4、留言
        switch (status) {
            case "1":
                if (currentLoginType == 2) {
                    checkHintText.setVisibility(View.GONE);
                    checkBtn.setVisibility(View.VISIBLE);
                } else {
                    checkHintText.setVisibility(View.VISIBLE);
                    checkBtn.setVisibility(View.GONE);
                }
                break;
            case "2":
                checkBtn.setVisibility(View.GONE);
                checkHintText.setVisibility(View.VISIBLE);
                checkHintText.setText("审核通过！    " + logisticsInfo.getUpdate_time());
                checkHintText.setTextColor(getResources().getColor(R.color.green_color));
                LogisticsInfo.InlandAgentBean inlandAgent = logisticsInfo.getInland_agent();
                if (inlandAgent!= null){
                     innerForwarderParent.setVisibility(View.VISIBLE);
                     forwarderNameText.setText(inlandAgent.getCompany_name());
                     forwarderContactText.setText(inlandAgent.getContact());
                     forwarderTelText.setText(inlandAgent.getPhone());
                }
                break;
            case "3":
                checkBtn.setVisibility(View.GONE);
                checkHintText.setVisibility(View.VISIBLE);
                checkHintText.setText("审核不通过！    " + logisticsInfo.getUpdate_time());
                checkHintText.setTextColor(getResources().getColor(R.color.red_text_color));
                if (currentLoginType == 1) {
                    ensureBtn.setVisibility(View.VISIBLE);
                    ensureBtn.setOnClickListener(new OnNoDoubleClickListener() {
                        @Override
                        protected void onNoDoubleClick(View v) {
                            OutboundAfterActivity.startAction(mContext,logisticsInfo);
                            finish();
                        }
                    });
                }
                break;
            case "4":
                checkBtn.setVisibility(View.GONE);
                checkHintText.setVisibility(View.VISIBLE);
                checkHintText.setText("审核有留言！    " + logisticsInfo.getRemark() + logisticsInfo.getUpdate_time());
                break;
        }
        orderNameText.setText(logisticsInfo.getTitle());
        orderCodeText.setText(logisticsInfo.getOrder_sn());
        batchText.setText(logisticsInfo.getBatch_sn());
        String batchInfo = logisticsInfo.getBatch_info().getItems();
        int batchInfoTotal = logisticsInfo.getBatch_info().getTotal();
        if (batchInfoTotal > 2){
            productMoreText.setVisibility(View.VISIBLE);
        }else {
            productMoreText.setVisibility(View.GONE);
        }
        productText.setText(StringUtil.isEmpty(batchInfo) ? "" : batchInfo.replace("--","\n"));
        allNumText.setText(logisticsInfo.getPackage_num());
        timeText.setText(logisticsInfo.getUpdate_time());
        List<LogisticsInfo.CheckRecordBean.LogisticsReleaseBean> logisticRemark = logisticsInfo.getCheck_record().getLogistics_release();
        if (logisticRemark != null && logisticRemark.size() > 0){
//            int size = logisticRemark.size();
            LogisticsInfo.CheckRecordBean.LogisticsReleaseBean itemRemark = logisticRemark.get(0);
            contactNoteText.setText(itemRemark.getRemark());
        }
        productMoreText.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ProductListViewWebViewActivity.startAction(mContext,logisticsInfo.getOrder_id(),logisticsInfo.getBatch_id());
            }
        });
//        final String attach = logisticsInfo.getAttach();
//        if (StringUtil.isEmpty(attach)) {
//            letterParent.setVisibility(View.GONE);
//        } else {
//            letterParent.setVisibility(View.VISIBLE);
//            ImageLoaderUtils.display(mContext, authLetterImg, attach);
//            authLetterImg.setOnClickListener(new OnNoDoubleClickListener() {
//                @Override
//                protected void onNoDoubleClick(View v) {
//                    ArrayList<String> paths = new ArrayList<String>();
//                    paths.add(attach);
//                    PhotosShowActivity.startAction(mContext,paths,0);
//                }
//            });
//        }
    }

    @OnClick({R.id.check_btn,R.id.check_result_parent,R.id.gnhd_parent,R.id.submit_check_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.check_btn:
                String batchId = logisticsInfo.getBatch_id();
                int shipStatus = Integer.parseInt(logisticsInfo.getShip_status());
                ForwarderWebViewActivity.startAction(mContext,1,batchId,shipStatus,logisticsInfo.getShip_list(),1000);
//                checkParent.setVisibility(View.VISIBLE);
                break;
//            case R.id.check_result_parent:
//                //审核结果
//                DialogUtil.showSelectCheckReusltDialogView(mContext, new EnsureCallBack() {
//                    @Override
//                    public void getCallBack(int type) {
//                      // 0:通过 1：不通过
//                        if (type == 0){
//                            checkResultText.setText("通过");
//                            checkResultStatus = 2;
//                            gnhdParent.setVisibility(View.VISIBLE);
//                        }else {
//                            checkResultText.setText("不通过");
//                            checkResultStatus = 3;
//                            gnhdParent.setVisibility(View.GONE);
//                        }
//                    }
//                });
//                break;
//            case R.id.gnhd_parent:
//                //国内货代
//                String companyId = logisticsInfo.getCompany_id();
//                ForwarderWebViewActivity.startAction(mContext,1,companyId,1000);
//                break;
//            case R.id.submit_check_btn:
//                //提交审核数
//                if (checkResultStatus == 0){
//                    showShortToast("请选择审核结果！");
//                    return;
//                }
//                String remark = noteEdit.getText().toString().trim();
//                if (StringUtil.isEmpty(remark)) {
//                    showShortToast("请输入备注信息！");
//                    return;
//                }
//                mPresenter.outboundSubmit(logisticsInfo.getBatch_id(), Integer.parseInt(logisticsInfo.getShip_status()), checkResultStatus, remark);
//                break;
        }
    }

    @Override
    public void outboundSubmitSuccess() {
        finish();
    }

    @Override
    public void outboundSubmitFail() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1000:
                    String result =  data.getStringExtra("result");
//                    showShortToast(result);
                    finish();
                    break;
            }
        }
    }
}
